/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aliasMatching.controller;

import aliasMatching.model.Alias;
import aliasMatching.model.TestTableModel;
import aliasMatching.view.MyTableCellRender;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import rdfparser.db.DBConnection;
import rdfparser.util.ExecutionTimeCalculate;

/**
 *
 * @author amendrashrestha
 */
public class TestFile {

    static List randomUsers;
    static int[] users;
    //static int[] users = {4};
    static List userPosts = new ArrayList();
    static List commonPost = new ArrayList();
    static List userPostsTime = new ArrayList();
    static List firstUserPost, secondUserPost, firstUserPostTime, secondUserPostTime;
    static String firstUser, secondUser;
    static List<List> userwithPost;
    static TestTableModel mdl;
    private static JTable displayResult;
    private static JScrollPane scrollPane;
    public List firstIndexUser;

    public TestFile() {
        mdl = new TestTableModel();
    }

    /**
     * selecting only users who has posted more than 60 posts
     * @param ID
     * @throws SQLException
     */
    public List getAllUsers() throws SQLException {
        List databaseUsersID = new ArrayList();
        PreparedStatement ps;
        ResultSet rs;
        Connection con;

         /*String selectUserQuery = "SELECT MemberID FROM tbl_member_all a," +
                " (SELECT MakerID FROM tbl_posts group by MakerID" +
                " HAVING count(MakerID) > 90) b" +
                " WHERE a.MemberID = b.MakerID" +
                " order by a.MemberID";*/
        
        String selectUserQuery = 
                " SELECT distinct MakerID FROM tbl_posts " +
                " group by MakerID " + 
                //" HAVING count(MakerID) > 60 " +
               // " where MakerID > 82406 " +
                " order by MakerID" +
               " limit 50";              

        con = DBConnection.getDBConnection();
        System.out.println(selectUserQuery);
        ps = con.prepareStatement(selectUserQuery);
        rs = ps.executeQuery();
        while (rs.next()) {
            databaseUsersID.add(rs.getInt(1));
        }
        return databaseUsersID;
    }

    /**
     * get 10 random users from list of users
     * @return
     * @throws SQLException
     */
    public List getRandomUsers(List user) throws SQLException {
        randomUsers = new ArrayList();
        int userSize = user.size();
        //firstIndexUser = new ArrayList(); 
        //Collections.shuffle(user);
        //Collections.sort(user);   

        for (int i = 0; i < userSize; i++) {
            int tempUser = Integer.parseInt(user.get(i).toString());
            randomUsers.add(tempUser); 
        }
        //firstIndexUser.add(user.get(0).toString());
        return randomUsers;
    }

    public static void divideUser(String ID) throws SQLException {
        dividePost(ID);
        firstUser = ID + "_A";
        secondUser = ID + "_B";
    }

    /**
     * Dividing posts between users
     * @param args
     * @throws SQLException
     */
    public static void dividePost(String ID) throws SQLException {

         //long postStartTime = System.currentTimeMillis();
       userPosts = Alias.getAllPosts(Integer.parseInt(ID));
        //long postEndTime = System.currentTimeMillis();
         //System.out.println("Post Execution Time: " + ExecutionTimeCalculate.returnTime(postStartTime, postEndTime) + " min");
        userPostsTime = Alias.getAllPostTime(Integer.parseInt(ID));
        


       firstUserPost = new ArrayList();
        secondUserPost = new ArrayList();
        firstUserPostTime = new ArrayList();
        secondUserPostTime = new ArrayList();

        for (int i = 0; i < userPostsTime.size(); i++) {
            //String date = userPostsTime.get(i).toString();
            //int day = Integer.parseInt(date.substring(8, 10));
            /**
             * the first 50% of posts are assigned to first split user and
             * rest of 50% to second user.
             */
            /*double dividePost = (50 * userPosts.size())/100;
            if(i < dividePost){
            firstUserPost.add(userPosts.get(i));
            firstUserPostTime.add(userPostsTime.get(i));
            }
            else{
            secondUserPost.add(userPosts.get(i));
            secondUserPostTime.add(userPostsTime.get(i));
            }*/
            /**
             * The odd number post are assigned to first split user and
             * even number are assigned to second split user.
             */
            if (i % 2 == 0) {
                firstUserPost.add(userPosts.get(i));
                firstUserPostTime.add(userPostsTime.get(i));
            } else {
                secondUserPost.add(userPosts.get(i));
                secondUserPostTime.add(userPostsTime.get(i));
            }
        }
    }

    public static void getStylometric(List user, TestTableModel testMdl) throws SQLException {

        List<Alias> styloUser = new ArrayList();

        TimeStylomTest se = new TimeStylomTest(user, null, testMdl);
        styloUser = se.aliases;
        int userSize = styloUser.size();
        Alias[] tempAlias_A = new Alias[userSize];
        Alias[] tempAlias_B = new Alias[userSize];

        String[] tempUser = new String[userSize * 2];

        for (int i = 0; i < userSize; i++) {
            tempUser[i] = styloUser.get(i).getUser();
            divideUser(tempUser[i]);

            tempAlias_A[i] = new Alias(firstUser);
            tempAlias_A[i].setPosts(firstUserPost);
            tempAlias_A[i].setPostTime(firstUserPostTime);
            //styloUser.add(i, tempAlias_A[i]);

            tempAlias_B[i] = new Alias(secondUser);
            tempAlias_B[i].setPosts(secondUserPost);
            tempAlias_B[i].setPostTime(secondUserPostTime);
            styloUser.add(tempAlias_B[i]);
            //System.out.println("ALias A:" + tempAlias_B[i]);
        }
        for (int j = 0; j < userSize; j++) {
            styloUser.remove(0);
        }

        /**
         * put _A user in first index and send to stylo
         * after that remove that user and again add another
         * _A user from tempAlias_A list
         *
         */
        for (int i = 0; i < userSize; i++) {
            long styloStartTime = System.currentTimeMillis();
            styloUser.add(0, tempAlias_A[i]);
            se.executeStylo();
            styloUser.remove(0);
            long styloEndTime = System.currentTimeMillis();
            System.out.println("Execution Time: " + ExecutionTimeCalculate.returnTime(styloStartTime, styloEndTime) + " min");
            System.out.println("----------------");
        }
    }

    public static void main(String args[]) throws SQLException {
        long userStartTime = System.currentTimeMillis();

        TestFile tf = new TestFile();
        JScrollPane createTestTable = new JScrollPane();
        //int i = 0;
        //List randomtestUsers = new ArrayList();

        List user = tf.getAllUsers();
        //List randomtestUsers = tf.getRandomUsers(user);
        createTestTable = createTestTable(mdl, user);
        /*while (i < 1) {

        List user = tf.getAllUsers();
        List randomtestUsers = tf.getRandomUsers(user);
        //randomtestUsers.add(4);
        //randomtestUsers.add(336);
        createTestTable = createTestTable(mdl, randomtestUsers);
        i++;
        }*/

        JLabel test = new JLabel("Test for Stylometric and Time Equaltiy");
        test.setSize(200, 100);

        JFrame testFrame = new JFrame("Test");
        JPanel tablePanel = new JPanel();
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tablePanel.add(test, BorderLayout.NORTH);
        tablePanel.add(createTestTable, BorderLayout.CENTER);

        testFrame.add(tablePanel);
        testFrame.setSize(new Dimension(550, 700));
        testFrame.setVisible(true);
        long userEndTime = System.currentTimeMillis();
        System.out.println("Total Execution Time: " + ExecutionTimeCalculate.returnTime(userStartTime, userEndTime) + " min");
    }

    private static JScrollPane createTestTable(TestTableModel mdl, List user) throws SQLException {

        displayResult = new JTable(mdl);
        displayResult.setBackground(Color.white);
        displayResult.setAutoscrolls(true);
        displayResult.getTableHeader().setReorderingAllowed(false);
        scrollPane = new JScrollPane(displayResult);
        scrollPane.setPreferredSize(new Dimension(500, 640));
        displayResult.setFillsViewportHeight(true);
        getStylometric(user, mdl);

        return scrollPane;
    }
}
